package com.techsoul.module.tms.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.constant.ApiConstants;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.tms.mapper.TPoPurchaseOrderItemMapper;
import com.techsoul.module.tms.pojo.entity.TPoPurchaseOrderItem;
import com.techsoul.module.tms.pojo.param.TPoPurchaseOrderItemSaveParam;
import com.techsoul.module.tms.pojo.param.TPoPurchaseOrderItemSearchParam;
import com.techsoul.module.tms.pojo.vo.TPoPurchaseOrderItemVo;
import com.techsoul.module.tms.service.ITFoTransitRouteService;
import com.techsoul.module.tms.service.ITPoPurchaseOrderItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 采购订单明细表 服务类
 * </p>
 *
 * @author liuen
 * @since 2019-09-10
 */
@DS("tms")
@Service
@Transactional(readOnly = true)
public class TPoPurchaseOrderItemServiceImpl implements ITPoPurchaseOrderItemService {

    @Autowired
    private TPoPurchaseOrderItemMapper mapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IdWorker idWorker;
    @Override
    @Transactional(readOnly = false)
    public String insert(TPoPurchaseOrderItemSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TPoPurchaseOrderItem entity = new TPoPurchaseOrderItem();
        BeanUtils.copyProperties(params, entity);
        entity.setId( idWorker.nextId() + "" )
                .preInsert(user.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }
    @Override
    @Transactional(readOnly = false)
    public void delete(String id) {
        this.mapper.deleteById(id);
    }
    @Override
    @Transactional(readOnly = false)
    public void update(String id, TPoPurchaseOrderItemSaveParam params) {
        TPoPurchaseOrderItem entity = new TPoPurchaseOrderItem();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entity);
        entity.setId(id).preUpdate(user.getId());
        this.mapper.updateById(entity);
    }
    @Override
    public TPoPurchaseOrderItem findById(String id) {
        return this.mapper.selectById(id);
    }
    @Override
    public List<TPoPurchaseOrderItem> findAll() {
        return this.mapper.selectList(new QueryWrapper<>());
    }
    @Override
    public List<TPoPurchaseOrderItemVo> findAll(TPoPurchaseOrderItemSearchParam params) {
        return this.mapper.findAll(params);
    }
    @Override
    public IPage<TPoPurchaseOrderItemVo> findAll(TPoPurchaseOrderItemSearchParam params, Integer currentPage, Integer pageSize) {
        return this.mapper.findAll(new Page<>(currentPage, pageSize), params);
    }

    /**
     * 根据订单主表ID逻辑删除明细
     */
    @Override
    public void deleteByPurchaseOrderId(String purchaseOrderId) {
        // 更新的字段
        TPoPurchaseOrderItem purchaseOrderItem = new TPoPurchaseOrderItem();
        purchaseOrderItem.setIsValid(ApiConstants.N);
        // 更新条件
        UpdateWrapper<TPoPurchaseOrderItem> uw = new UpdateWrapper();
        uw.eq("purchase_order_id", purchaseOrderId);
        this.mapper.update(purchaseOrderItem, uw);
    }
}
